package com.example.cake.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.cake.common.Result;
import com.example.cake.entity.DTO.SysClassifyDTO;
import com.example.cake.entity.SysCake;
import com.example.cake.entity.SysClassify;
import com.example.cake.service.SysCakeService;
import com.example.cake.service.SysClassifyService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.List;

@RestController
@RequestMapping("/classify")
public class SysClassifyController {
    @Autowired
    private SysClassifyService sysClassifyService;
    @Autowired
    private SysCakeService sysCakeService;


    @PostMapping("/list")
    public Result list(@RequestBody SysClassifyDTO sysClassifyDTO) {
        LambdaQueryWrapper<SysClassify> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(sysClassifyDTO.getName()),SysClassify::getName,sysClassifyDTO.getName());
        Page<SysClassify> page = new Page<>(sysClassifyDTO.getPage(), sysClassifyDTO.getLimit());
        return Result.success(sysClassifyService.page(page,lqw));
    }
    @GetMapping("/getList")
    public Result getList() {
        List<SysClassify> list = sysClassifyService.list();
        list.forEach(item->{
            LambdaQueryWrapper<SysCake> lqw = new LambdaQueryWrapper<>();
            lqw.eq(SysCake::getClassifyId,item.getId());
            List<SysCake> sysCakes = sysCakeService.list(lqw);
            item.setCakeList(sysCakes);
        });
        return Result.success(list);
    }
    @GetMapping("/getNameList")
    public Result getNameList() {
        List<SysClassify> list = sysClassifyService.list();
        return Result.success(list);
    }

    @GetMapping("/listByValue")
    public Result listByValue() {
        List<SysClassify> list = sysClassifyService.list();
        list.forEach(item->{
            item.setLabel(item.getName());
            item.setValue(item.getId().toString());
        });
        return Result.success(list);
    }

    @PostMapping("/add")
    public Result add(@RequestBody SysClassify sysClassify) {
        sysClassify.setCreateTime(DateUtil.toLocalDateTime(new Date()));
        sysClassify.setUpdateTime(DateUtil.toLocalDateTime(new Date()));
        return Result.success(sysClassifyService.save(sysClassify));
    }

    @PostMapping("/remove")
    public Result add(@RequestBody Integer id) {
        return Result.success(sysClassifyService.removeById(id));
    }

    @PostMapping("/update")
    public Result update(@RequestBody SysClassify sysClassify) {
        sysClassify.setUpdateTime(DateUtil.toLocalDateTime(new Date()));
        return Result.success(sysClassifyService.updateById(sysClassify));
    }
}
